<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>教务系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../js/layui/css/layui.css" media="all">

    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<div class="demoTable" style="text-align:center">
    姓名：
    <div class="layui-inline">
        <input class="layui-input" id="student_name" autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    <#--    <div class="layui-btn-container">-->
    <#--        <button   class="layui-btn layui-btn-warm" lay-event="add">-->
    <#--            <i class="layui-icon" ></i> 添加-->
    <#--        </button>-->
    <#--    </div>-->
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看处分原因</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="../js/layui/layui.js" charset="utf-8"></script>
<script src="/js/layui/jquery.1.12.4.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    /**
     {
"code": 0,
"msg": "",
"count": 1000,
"data": [{}, {}]
}
     */
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test'
            , url: '/admin/violate/list'
            , toolbar: '#toolbarDemo'
            , title: '教师信息表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'handler', title: '处理人', width: 120}
                , {field: 'student', title: '学生姓名', width: 120}
                , {field: 'score', title: '扣分', width: 120}
                , {field: 'reason', title: '处分原因', width: 300}
                , {field: 'created', title: '加入时间', width: 120}
                /* ,{field:'email', title:'邮箱', width:150, edit: 'text', templet: function(res){
                         return '<em>'+ res.email +'</em>'
                     }}*/
                /* ,{field:'sex', title:'性别', width:80, edit: 'text', sort: true}*/

                // ,{field:'experience', title:'积分', width:80, sort: true}
                // ,{field:'ip', title:'IP', width:120}
                // ,{field:'logins', titl-e:'登入次数', width:100, sort: true}
                // ,{field:'joinTime', title:'加入时间', width:120}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 230}
            ]]
            , page: true
            , id: 'testReload'
        });

        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');

                //执行重载
                table.reload('testReload', {
                    url: '/admin/violate/list1'
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        studentName: '' ? '' : $('#student_name').val()
                    }
                }, 'data');

            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            if ( $('#student_name').val().length>=1 && $('#student_name').val().length < 8) {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            } else {
                layer.msg("名字长度不能小于1也不能大于8");

            }

        })
        ;

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event == "add") {
                layer.open({
                    type: 1,
                    title: "添加",
                    closeBtn: false,
                    shift: 2,
                    area: ['500px', '500px'],
                    shadeClose: true,
                    anim: 3,
                    content: $("#add-tou")//这里content是一个普通的String

                });
            }
            // switch (obj.event) {
            //     case 'getCheckData':
            //         var data = checkStatus.data;
            //         layer.alert(JSON.stringify(data));
            //         break;
            //     case 'getCheckLength':
            //         var data = checkStatus.data;
            //         layer.msg('选中了：' + data.length + ' 个');
            //         break;
            //     case 'isAll':
            //         layer.msg(checkStatus.isAll ? '全选' : '未全选');
            //         break;
            //
            // }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: "POST",
                        url: "/admin/violate/del",
                        data: {id: data.id},
                        success: function (msg) {
                            window.location.reload();
                            layer.close(index);
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                $.ajax({
                    type: "GET",
                    url: "/admin/cookieInfo/edit",
                    data: {id: data.id},
                    success: function (msg) {
                        window.location.href="/eidt";
                    }
                });


            }
        });
    })
    ;
</script>

<div id="add-tou" style="display: none;">
    <form class="layui-form" id="addvio" action="" method="post">

        <div class="layui-form-item center">
            <label class="layui-form-label" style="width: 100px">学生姓名：</label>
            <div class="layui-input-block">
                <input type="text" name="name" required value="" style="width: 240px" lay-verify="required"
                       placeholder="请输入名字" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item center">
            <label class="layui-form-label" style="width: 100px">扣分：</label>
            <div class="layui-input-block">
                <input type="number" name="deduct_marks" required value="" style="width: 240px" lay-verify="required"
                       placeholder="请输入分数" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">处分原因：</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="reason" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-bloc k">
                <#--<button class="layui-btn"  >立即添加</button>-->
                <input class="layui-btn" lay-submit onclick="addinfo()" lay-filter="save" value="立即添加">
            </div>
        </div>
    </form>
</div>
<script>

    function addinfo() {
        $.ajax({
            type: "POST",
            url: "/admin/violate/add",
            data: $('#addvio').serialize(),
            success: function (msg) {
                if (msg.code == 200) {
                    layer.msg('添加成功！');
                    window.location.replace("/violate");
                }
                if (msg.code == 201) {
                    layer.msg('该学生积分已不足!');
                }
                if (msg.code == 202) {
                    layer.msg('扣0分是没有意义的。');
                }
                if (msg.code == 202) {
                    layer.msg('扣0分是没有意义的。');
                }
                if (msg.code == 203) {
                    layer.msg('该学生积分已为0！');
                }
            },

        });
    };

    function updateinfo() {
        $.ajax({
            type: "POST",
            url: "/admin/violate/update",
            data: $('#add-form').serialize(),
            success: function (msg) {
                if (msg.code == 200) {
                    layer.msg('修改成功！');
                }
            },

        });
    }
</script>

</body>
</html>
</div>
</body>
</html>
